#!usr/bin/env python
#coding=utf8

from tuangou.models.tuan_models import SystemKeyValue
from tuangou.common_logic import commutil

def add_one(key, value):
    s = SystemKeyValue()
    s.key = key
    s.value = value
    s.save()

def update_by_key(key, value):
    keyvalue = get_by_key(key)
    if keyvalue:
        keyvalue.value = value
        keyvalue.save()
        
def update_by_id(id, value):
    keyvalue = get_by_id(id)
    if keyvalue:
        keyvalue.value = value
        keyvalue.save()
    
def get_by_key(key):
    try:
        keyvalue = SystemKeyValue.objects.filter(key=key)[0]
    except:
        keyvalue = None
        
    return keyvalue  

def get_by_id(id):  
    try:
        keyvalue = SystemKeyValue.objects.get(id=id)
    except:
        keyvalue = None
        
    return keyvalue  

def del_by_key(key):
    SystemKeyValue.objects.filter(key=key).delete()

def del_by_id(id):    
    SystemKeyValue.objects.filter(id=id).delete()







    
    